import { BeforeInsert, Column, Entity, JoinTable, ManyToMany } from "typeorm";
import * as crypto from 'crypto';
import encry from '../../utils/crypto';
import { BaseEntity } from '../../utils/base.entity';

@Entity('user')
export class User extends BaseEntity {
  @Column({ comment: '手机号码' })
  mobile: string;
  @Column({ comment: '密码' })
  password: string;
  @Column({ comment: '加密盐值' })
  salt: string;
  @Column({ comment: '昵称' })
  nick: string;
  @Column({ comment: '头像' })
  avatar: string;

  @ManyToMany(() => User)
  @JoinTable()
  friends: User[];

  @BeforeInsert()
  beforeInsert() {
    this.salt = crypto.randomBytes(16).toString('base64');
    this.password = encry(this.password, this.salt);
  }
}
